package com.hk.kindheartedness;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hk.kindheartedness.db.DBOpenHelper;
import com.hk.kindheartedness.util.DateUtil;
import com.hk.kindheartedness.util.ToastUtil;

import java.util.Date;

/**
 * 医生
 *
 * 更新患者病历业务处理
 */
public class DoctorUpdateCaseHistoryActivity extends AppCompatActivity implements View.OnClickListener {

    //    Initialize Toast
    private Toast toast;

    //    Initialize intent
    private Intent intent;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    private TextView userNumber,userName,userAge,userSex,departmentsName;

    private EditText userMainSuit,userPhi,previousHistory,drugAllergyHistory,personalAndFamilyHistory,healthCheckup,auxiliaryExamination,tentativeDiagnosis;

    private String loginDoctorId,registrationLogId,userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_update_case_history);
//        实例化
        dbOpenHelper = new DBOpenHelper(DoctorUpdateCaseHistoryActivity.this, "db_kindheartedness", null, 1);
//        获取当前登录医生Id
        Intent intent2 = getIntent();
        final Bundle bundle = intent2.getExtras();
        //        当前医生Id
        loginDoctorId = bundle.getString("loginDoctorId");
//        患者挂号Id
        registrationLogId = bundle.getString("registrationLogId");
        //        获取按钮控制权
        Button updateCaseHistoryBtn = findViewById(R.id.updateCaseHistoryBtn);
        updateCaseHistoryBtn.setOnClickListener(this);
//      获取各文本框和输入框控制权
        userNumber = findViewById(R.id.userNumber);
        userName = findViewById(R.id.userName);
        userAge = findViewById(R.id.userAge);
        userSex = findViewById(R.id.userSex);
        departmentsName = findViewById(R.id.departmentsName);
        userMainSuit = findViewById(R.id.userMainSuit);
        userPhi = findViewById(R.id.userPhi);
        previousHistory = findViewById(R.id.previousHistory);
        drugAllergyHistory = findViewById(R.id.drugAllergyHistory);
        personalAndFamilyHistory = findViewById(R.id.personalAndFamilyHistory);
        healthCheckup = findViewById(R.id.healthCheckup);
        auxiliaryExamination = findViewById(R.id.auxiliaryExamination);
        tentativeDiagnosis = findViewById(R.id.tentativeDiagnosis);
//      查询数据并回显
        findByRegistrationLogId();



    }

    /**
     * 通过患者挂号记录Id查询
     */
    private void findByRegistrationLogId() {
        SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
        //        通过患者挂号记录ID查询患者用户ID
        Cursor queryCaseHistory = readableDatabase.query("case_history", null, "registration_log_id = ?", new String[]{registrationLogId}, null, null, null);
        String userNumberToStr = null,userNameToStr = null,userAgeToStr = null,userSexToStr = null,departmentsNameToStr = null;
        String userMainSuitToStr = null,userPhiToStr = null,previousHistoryToStr = null,drugAllergyHistoryToStr = null,personalAndFamilyHistoryToStr = null;
        String healthCheckupToStr = null,auxiliaryExaminationToStr = null,tentativeDiagnosisToStr = null;
        while (queryCaseHistory.moveToNext()) {
            userNameToStr = queryCaseHistory.getString(1);
            userNumberToStr = queryCaseHistory.getString(2);
            userSexToStr = queryCaseHistory.getString(3);
            userAgeToStr = queryCaseHistory.getString(4);
            departmentsNameToStr = queryCaseHistory.getString(5);
            userMainSuitToStr = queryCaseHistory.getString(6);
            userPhiToStr = queryCaseHistory.getString(7);
            previousHistoryToStr = queryCaseHistory.getString(8);
            drugAllergyHistoryToStr = queryCaseHistory.getString(9);
            personalAndFamilyHistoryToStr = queryCaseHistory.getString(10);
            healthCheckupToStr = queryCaseHistory.getString(11);
            auxiliaryExaminationToStr = queryCaseHistory.getString(12);
            tentativeDiagnosisToStr = queryCaseHistory.getString(13);
        }
        userNumber.setText(userNumberToStr);userName.setText(userNameToStr);
        userAge.setText(userAgeToStr);userSex.setText(userSexToStr);
        departmentsName.setText(departmentsNameToStr);
        userMainSuit.setText(userMainSuitToStr);userPhi.setText(userPhiToStr);
        previousHistory.setText(previousHistoryToStr);
        drugAllergyHistory.setText(drugAllergyHistoryToStr);
        personalAndFamilyHistory.setText(personalAndFamilyHistoryToStr);
        healthCheckup.setText(healthCheckupToStr);
        auxiliaryExamination.setText(auxiliaryExaminationToStr);
        tentativeDiagnosis.setText(tentativeDiagnosisToStr);
    }

    /**
     * 按钮点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            更新患者病历
            case R.id.updateCaseHistoryBtn:
                updateCaseHistory();
                break;
            default:
                break;
        }

    }


    /**
     * 更新患者病历
     */
    private void updateCaseHistory() {
//        获取各文本框内容
        String userNumberToStr = userNumber.getText().toString();
        String userNameToStr = userName.getText().toString();
        String userAgeToStr = userAge.getText().toString();
        String userSexToStr = userSex.getText().toString();
        String departmentsNameToStr = departmentsName.getText().toString();
        String userMainSuitToStr = userMainSuit.getText().toString();
        String userPhiToStr = userPhi.getText().toString();
        String previousHistoryToStr = previousHistory.getText().toString();
        String drugAllergyHistoryToStr = drugAllergyHistory.getText().toString();
        String personalAndFamilyHistoryToStr = personalAndFamilyHistory.getText().toString();
        String healthCheckupToStr = healthCheckup.getText().toString();
        String auxiliaryExaminationToStr = auxiliaryExamination.getText().toString();
        String tentativeDiagnosisToStr = tentativeDiagnosis.getText().toString();
//        校验数据
        Boolean check = checkDate(
                userNumberToStr,userNameToStr,userAgeToStr,userSexToStr,departmentsNameToStr,
                userMainSuitToStr, userPhiToStr,previousHistoryToStr,drugAllergyHistoryToStr,personalAndFamilyHistoryToStr,
                healthCheckupToStr,auxiliaryExaminationToStr,tentativeDiagnosisToStr);
        if (check) {
            SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
//            字段关系映射封装
            ContentValues contentValues = new ContentValues();
            contentValues.put("main_suit",userMainSuitToStr);
            contentValues.put("phi",userPhiToStr);
            contentValues.put("previous_history",previousHistoryToStr);
            contentValues.put("drug_allergy_history",drugAllergyHistoryToStr);
            contentValues.put("personal_and_family_history",personalAndFamilyHistoryToStr);
            contentValues.put("health_checkup",healthCheckupToStr);
            contentValues.put("auxiliary_examination",auxiliaryExaminationToStr);
            contentValues.put("tentative_diagnosis",tentativeDiagnosisToStr);
            contentValues.put("patient_name",userNameToStr);
            contentValues.put("update_time",DateUtil.dateString(new Date()));
            long update = readableDatabase.update("case_history",contentValues,"registration_log_id = ?",new String[]{registrationLogId});
            if (update == 1) {
                toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"病历更新成功!");
                return;
            }
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"病历更新失败,请重新保存！");
            return;
        }


    }

    /**
     * 校验各参数是否正确或不为空
     * @param userNumberToStr 患者编号
     * @param userNameToStr 患者姓名
     * @param userAgeToStr 患者年龄
     * @param userSexToStr 患者性别
     * @param departmentsNameToStr 患者挂诊科室
     * @param userMainSuitToStr 主诉
     * @param userPhiToStr 现病史
     * @param previousHistoryToStr 既往史
     * @param drugAllergyHistoryToStr 药物过敏史
     * @param personalAndFamilyHistoryToStr 个人及家族史
     * @param healthCheckupToStr 体格检查
     * @param auxiliaryExaminationToStr 辅助检查
     * @param tentativeDiagnosisToStr 初步诊断
     * @return 返回true表示校验成功
     */
    private Boolean checkDate(
            String userNumberToStr, String userNameToStr, String userAgeToStr, String userSexToStr,String departmentsNameToStr,
            String userMainSuitToStr, String userPhiToStr, String previousHistoryToStr,
            String drugAllergyHistoryToStr, String personalAndFamilyHistoryToStr,
            String healthCheckupToStr, String auxiliaryExaminationToStr, String tentativeDiagnosisToStr
    ) {
        if (userNumberToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"患者编号不能为空！");
            return false;
        }
        if (userNameToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"患者姓名不能为空！");
            return false;
        }
        if (userAgeToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"患者年龄不能为空！");
            return false;
        }
        if (userSexToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"患者性别不能为空！");
            return false;
        }
        if (departmentsNameToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"挂诊科室不能为空！");
            return false;
        }
//      校验各输入参数
        if (userMainSuitToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"主诉不能为空！");
            return false;
        }
        if (userPhiToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"现病史不能为空！");
            return false;
        }
        if (previousHistoryToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"既往史不能为空！");
            return false;
        }
        if (drugAllergyHistoryToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"药物过敏史不能为空！");
            return false;
        }
        if (personalAndFamilyHistoryToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"个人及家族史不能为空！");
            return false;
        }
        if (healthCheckupToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"体格检查不能为空！");
            return false;
        }
        if (auxiliaryExaminationToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"辅助检查不能为空！");
            return false;
        }
        if (tentativeDiagnosisToStr.equals("")) {
            toast = ToastUtil.toastHint(toast,DoctorUpdateCaseHistoryActivity.this,"初步诊断不能为空！");
            return false;
        }
        return true;
    }

    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }

}